package com.Listeners;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.MultiAutoCompleteTextView;
import android.widget.ToggleButton;
import com.Adapters.Items.RowType;
import com.Animations.AnimUtil;
import com.Data.Likes;
import com.Data.Post;
import com.Data.User;
import com.Managers.DirManagerClass;
import com.Managers.FragmentManagerClass;
import com.Managers.UserManagerClass;
import com.TextFormat.TagEditTextView;
import com.TextFormat.UtilFonts;
import com.activities.ActivityComments;
import com.activities.ActivityMap;
import com.activities.ActivityProfileUser;
import com.activities.ImagePagerActivity;
import com.shareup.R;


public class PostItemsListeners {

    public static class CommentClickListener implements OnClickListener {

        private Post picture;

        public CommentClickListener(Post picture) {

            this.picture = picture;

        }

        @Override
        public void onClick(View v) {

            {
                Intent intent = new Intent(v.getContext(), ActivityComments.class);
                intent.putExtra("IDPICTURE", picture.getID());
                intent.putExtra("IDOWNER", picture.getUser().getID());

                String path = picture.getPathFull();
                if (!path.equals("null"))
                    intent.putExtra("path", DirManagerClass.getPathPicture(path));

                v.getContext().startActivity(intent);
                AnimUtil.getActivityTransition(FragmentManagerClass.aMain, true);
            }
        }
    }

    public static class ImagePostClickListener implements OnClickListener {

        int position;

        Uri uri;

        public ImagePostClickListener(String uri, int position) {

            this.position = position;
            this.uri = DirManagerClass.getPathFromPicture(uri);
        }

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(v.getContext(), ImagePagerActivity.class);
            intent.putExtra("POSITION", position);
            intent.putExtra("IDS", FragmentManagerClass.adapterPosts.compileIds());
            // v.getContext().startActivity(intent);

            AnimUtil.getActivityTransition(((Activity) v.getContext()), true);

        }

    }

    public static class LikeClickListener implements OnClickListener {

        private LikeCompleted callback;

        private Post picture;

        public LikeClickListener(Post picture,
                    LikeCompleted callbackLike) {

            this.callback = callbackLike;
            this.picture = picture;
        }

        @Override
        public void onClick(View v) {

            {
                ToggleButton tbut = (ToggleButton) v;
                //
                if (tbut.isChecked()) {

                    String[] data = new String[3];
                    data[0] = String.valueOf(picture.getID());
                    data[1] = String.valueOf(UserManagerClass.UserLogged.getID());
                    data[2] = String.valueOf(picture.getUser().getID());
                    Likes lk = new Likes(data);
                    lk.execute(Likes.ACTION_ADD);
                    int likesCounters = picture.getPictureCounters().getLikesCount();
                    likesCounters++;
                    picture.getPictureCounters().setLikesCount(likesCounters);
                    picture.getPictureCounters().setIsMine(1);

                }
                else {

                    String[] data = new String[3];
                    data[0] = String.valueOf(picture.getID());
                    data[1] = String.valueOf(UserManagerClass.UserLogged.getID());
                    data[2] = String.valueOf(picture.getUser().getID());
                    Likes lk = new Likes(data);
                    lk.execute(Likes.ACTION_DELETE);
                    int commentsCounter = picture.getPictureCounters().getLikesCount();
                    commentsCounter--;
                    picture.getPictureCounters().setLikesCount(commentsCounter);
                    picture.getPictureCounters().setIsMine(0);
                    //
                }
                callback.Completed();

            }
        }
    }

    public interface LikeCompleted {

        void Completed();
    }

    public static class LocationClickListener implements OnClickListener {

        private boolean mine;

        private Post picture;

        private int type;

        public LocationClickListener(Post pic, RowType type) {

            this.type = type.ordinal();
            this.picture = pic;
            if (pic.getUser().getID() == UserManagerClass.UserLogged.getID())
                mine = true;

        }

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(v.getContext(), ActivityMap.class);
            Bundle extras = new Bundle();
            extras.putDouble("latitud", picture.getGeoTag().getLatitude());
            extras.putDouble("longitud", picture.getGeoTag().getLongitude());
            extras.putString("owner", picture.getUser().getRealName());

            String path = picture.getPathThumb();
            if (type == RowType.TEXT_POST.ordinal())
                path = picture.getUser().getProfilePicture();

            extras.putString("picture", path);
            extras.putString("local", picture.getGeoTag().toString());
            extras.putBoolean("mine", mine);
            extras.putInt("type", type);
            intent.putExtras(extras);

            v.getContext().startActivity(intent);
            AnimUtil.getActivityTransition(FragmentManagerClass.aMain, true);

        }
    }

    public static class ProfileClickListener implements OnClickListener {

        private Activity activity;

        private User Logged;

        private Post picture;

        public ProfileClickListener(Post picture, Activity activity) {

            this.picture = picture;
            this.Logged = UserManagerClass.UserLogged;
            this.activity = activity;
        }

        @Override
        public void onClick(View v) {

            {
                int id = picture.getUser().getID();
                String pict = picture.getUser().getProfilePicture();
                String name = picture.getUser().getRealName();
                if (id == Logged.getID()) {
                    pict = Logged.getProfilePicture();
                    name = Logged.getRealName();
                }
                //
                Intent intent = new Intent(v.getContext(), ActivityProfileUser.class);
                intent.putExtra("imgprofile", pict);
                intent.putExtra("nameprofile", name);
                intent.putExtra("IDUSER", id);
                //
                activity.startActivity(intent);
                AnimUtil.getActivityTransition(activity, true);
            }
        }
    }

    public static class TagClickListener implements OnClickListener {

        private String tags = "";

        int position;

        public TagClickListener(Post pic) {

            this.tags = pic.getTags();

        }

        @Override
        public void onClick(View vs) {

            AlertDialog.Builder dg = new AlertDialog.Builder(vs.getContext());
            LayoutInflater inflater = (LayoutInflater) vs.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.dialog_show_tags, null);
            // ///////////////////////////////////////////////////////////////////////////////////////////////////////
            TagEditTextView mu = (TagEditTextView) v.findViewById(R.id.tagtext);
            mu.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
            mu.setFocusable(false);
            // /////////////////////////////////////////////////////////////////////////
            UtilFonts.setTextRegularFont(mu);
            // //////////////////////////////////////////////////////////////////
            dg.setView(v);
            mu.setText(tags);
            if ((tags.length() > 1))
                UtilFonts.setChips(mu);
            AlertDialog alert = dg.create();
            alert.show();
            //

        }
    }

}
